A Frontend Developer's notebook, working on more friendly feature here. 🚧
Ula Chao
Taiwan

When to use css background or html img tag

2021-04-01

# css

There are two ways to set a image in css,

  1. background in <div> tag
  2. <img> tag

css background

html <img>

  • attributes: (common)
    • alt: display this string when image broken / for SEO
    • src: the source of the image (url)
    • width: px
    • height: px

When to use

  • <img>: When the image is an important data for your content.
  • background: When the image is a decoration in your UI.

Sources